function formValidator(frmId, rules, messages) {
	this.validation = function(){
		var result = validate(rules);
		return result;
	};
	
	var validate = function(rules, fieldId){
		var rtnValue = true;
		
		for(i in rules){			
			if(typeof rules[i] === "object"){
				rtnValue = validate(rules[i], i);
				if(!rtnValue) break;

			}else{
				var jsonQuery = new Array();
				if(fieldId != null && fieldId != "undefined"){
					jsonQuery["query"] = "messages."+fieldId+"."+i;
					jsonQuery["fieldId"] = fieldId;
					jsonQuery["key"] = i;
					jsonQuery["value"] = rules[i];
					
				}else{
					jsonQuery["query"] = "messages."+i;
					jsonQuery["fieldId"] = i;
					jsonQuery["key"] = rules[i];
					jsonQuery["value"] = "";
				}
				
				if($("#"+frmId + " #"+jsonQuery.fieldId).length == 0) continue;

				rtnValue = comment(jsonQuery);
				if(!rtnValue) break;
			}
		}
	    
		return rtnValue;
	};
	
	var comment = function(jsonQuery){
		var rtnValue = true;
		var query = jsonQuery.query;
		var fieldId = jsonQuery.fieldId;
		var key = jsonQuery.key;
		var value = jsonQuery.value;
		var obj = $("#"+frmId + " #"+fieldId);
		var msg = eval(query);
		
		if(key == "required"){
			if(typeof value === "boolean" && !value){
			}else{ if($(obj).val().length == 0) rtnValue = msgContainer(msg, $(obj));
			}			
		}else if(key == "minlength"){ if($(obj).val().length < value) rtnValue = msgContainer(msg, $(obj));			
		}else if(key == "maxlength"){ if($(obj).val().length > value) rtnValue = msgContainer(msg, $(obj));
		}else if(key == "expression"){
			var exp = new RegExp(eval(value));
			if(!exp.test($(obj).val()))  rtnValue = msgContainer(msg, $(obj));
		}

		return rtnValue;
	};

	var msgContainer = function(msg, obj){
		var rtnValue = false;		
		if(msg == null || msg == "undefined") msg = "Unknown internal error!!! \nThe error message is nothing.";
		
		alert(msg);
		$(obj).focus();
		return rtnValue;
	};
	
}


